// pages/createTrace/createTrace.js
var util = require('../../utils/util');
var app = getApp();//引入全局变量

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: wx.getStorageSync('user').userId,
    trace: [],
    trace_dy: [],
    inputValue: '',
    inputValue_dy: '',
    flag_trace:false,
    imgUrl:null,//保存拼接起来的url并上传
  },
  user:null,
  /**选择图片预览在框中 */
  upload_editor: null,
  img:null,
  flag_isclick:null,
  selectImageUpLoad:function () {
     this.chooseImageTap()
  },

  chooseImageTap:function(){
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册选择','拍照'],
      itemColor:"#000000",
      success:function(res){
        if(!res.cancel){
          if(res.tapIndex == 0){
            that.chooseWxImage('album')
          }else if(res.tapIndex ==1){
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  //图片本地路径
  chooseWxImage:function(type){
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType:['original','compressed'],
      sourceType:[type],
      success:function(res){
        console.log(res.tempFilePaths[0]);//返回的路径
        that.upImgs(res.tempFilePaths[0],0);//调用上传方法
        that.setData({
          ImagePath:res.tempFilePaths[0]
        })
      }
    })
  },
  //上传服务器
  upImgs:function(imgurl,index){
    var that = this;
    wx.uploadFile({
      filePath: imgurl,
      name: 'image',
      url: 'https://api.lzjlw.top:3030/residential/upload',
      header:{
        'content-type':'multipart/form-data'
      },
      success:function(res){
        console.log(res)
        that.data.imgUrl = `${app.globalData.url}`+res.data
        console.log(that.data.imgUrl)
      }
    })
  },
  //删除图片
  deleteImg:function(){
    var that = this
    wx.showModal({
      cancelColor: '#000000',
      title:'提示',
      content:'是否删除',
      success:function(res){
        if(res.confirm){
          that.setData({
            ImagePath:null
          })
          that.data.imgUrl = null
          console.log(that.data.imgUrl)
        }else if(res.cancel){
          console.log('用户点击取消')
        }
      }
    })
  },

  //保存足迹
  uploadTrace:function(e){
    let that = this;//that 是整个页面
    app.inputValue_dy = that.data.inputValue
    console.log(app.inputValue_dy + '动态inputValue获取到了')

    if(that.data.inputValue.length == 0 && that.data.imgUrl == null){
      wx.showModal({
        title: '提示',
        content: '内容不能为空',
        success: function (res) {
          if (res.confirm) {//这里是点击了确定以后
            console.log('用户点击确定')
          } else {//这里是点击了取消以后
            console.log('用户点击取消')
          }
        }
      })
    }else{
      that.data.trace.unshift({
        userId: this.data.userId,
        content: app.inputValue_dy,
        public_Time: util.formatTime(new Date()),
        flag_trace:false,
      })

      console.log((that.data.trace.content)==null)
      app.trace_dy = that.data.trace;//将保存了自己上传的信息的数组存到全局参数trace_dy中

      // 将自己保存的动态数据上传服务器
      wx.request({
        url: 'https://api.lzjlw.top:3030/residential/community_dynamic?type=3',
        method:'POST',
        data: {
          userId: wx.getStorageSync('user').userId,
          content: app.inputValue_dy,
          imgUrl: this.data.imgUrl,
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
          console.log('我的动态信息上传成功')
        }
      })
      wx.switchTab({
        url: '/pages/community/community',
      })

    }

  },
  //输入框获得焦点时触发
  bindInputBlur: function (e) {
    console.log(e)
    this.data.inputValue = e.detail.value;
    // console.log(this.data.inputValue + 'imbind')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    this.user = wx.getStorageSync('user')
    console.log(this.user)

    let query = wx.createSelectorQuery()
    query.select('#upload_preImg').context(res => {
      this.upload_preImg = res.context
      console.log(this.upload_preImg)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(util.formatTime(new Date()))
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})